#include <stdio.h>
#include <unistd.h>
#include "hi_timer.h"
#include "hi_gpio.h"
#include "dbg.h"
#include "hi_spi.h"
#include "hi_pwm.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "button_module.h"
#include "heater.h"

//GPIO5、7对应按键，6对应报警器
#define HI_GPIO_IDX_5 5
#define HI_GPIO_IDX_7 7
#define HI_GPIO_IDX_6 6

float TEMP_MAX = 40;        //报警温度
int TEMP_WAITTIME = 30;     //等待冷却的时间(s)

enum ButtonState{
    HEATER_OFF = 0,         //加热丝关闭(默认)
    HEATER_ON,
};
enum Button2State{
    HEATING_MODE = 1,       //加热模式(默认)
    SETTING,                //设置高温阈值
    TEMP_UP,                //最高温度加
    TEMP_DOWN,              //最高温度减
    SAVE,                   //保存设置
};
enum ButtonState g_ButtonState = HEATER_OFF;        //初始按键为关
enum ButtonState nextState = HEATER_ON;
enum Button2State g_Button2State = HEATING_MODE;    //初始按键2为加热
enum Button2State next2State = SETTING;

unsigned int lastTickCount;
static void OnButtonPressed(char *arg)  //按键1中断函数
{
    (void) arg;
    //按键消抖
    unsigned int tickCount = osKernelGetTickCount();
    unsigned int count = tickCount - lastTickCount;
    lastTickCount = tickCount;
    if(count < 50){
        printf("-----return\n");
        return;
    }
    //按下按键的状态切换
    printf("g_ButtonState = %d\n", g_ButtonState);
    switch(g_ButtonState)
    {
        case HEATER_ON:
            if(g_Button2State == TEMP_UP){
                TEMP_MAX++;
                printf("TEMP is changed+ !\r\n");
            }
            else if(g_Button2State == TEMP_DOWN){
                TEMP_MAX--;
                printf("TEMP is changed- !\r\n");
            }
            else if(g_Button2State == SAVE){
                g_Button2State = SETTING;
                printf("TEMP is SAVE !\r\n");
            }
            else if(g_Button2State == SETTING){
                g_Button2State = TEMP_UP;
            }
            nextState = HEATER_OFF;
            printf("---heater OFF !\r\n");
            break;
        case HEATER_OFF:
            if(g_Button2State == TEMP_UP){
                TEMP_MAX++;
                printf("TEMP is changed+ !\r\n");
            }
            else if(g_Button2State == TEMP_DOWN){
                TEMP_MAX--;
                printf("TEMP is changed- !\r\n");
            }
            else if(g_Button2State == SAVE){
                g_Button2State = SETTING;
                printf("TEMP is SAVE !\r\n");
            }
            else if(g_Button2State == SETTING){
                g_Button2State = TEMP_UP;
            }
            nextState = HEATER_ON;
            printf("---heater ON !\r\n");
            break;
        default:
            break;
    }
    g_ButtonState = nextState;
    printf("nextState = %d\n", nextState);
}
static void OnButton2Pressed(char *arg)  //按键2中断函数
{
    (void) arg;
    //按键消抖
    unsigned int tickCount = osKernelGetTickCount();
    unsigned int count = tickCount - lastTickCount;
    lastTickCount = tickCount;
    if(count < 50){
        printf("-----return\n");
        return;
    }
    //按下按键的状态切换
    printf("g_Button2State = %d\n", g_Button2State);
    switch(g_Button2State)
    {
        case HEATING_MODE:
            next2State = SETTING;
            printf("---SETTING !\r\n");
            break;
        case SETTING:
            next2State = HEATING_MODE;
            printf("---HEATING_MODE !\r\n");
            break;
        case TEMP_UP:
            next2State = TEMP_DOWN;
            printf("---TEMP_DOWN !\r\n");
            break;
        case TEMP_DOWN:
            next2State = SAVE;
            printf("---SAVE !\r\n");
            break;
        case SAVE:
            next2State = TEMP_UP;
            printf("---TEMP_UP !\r\n");
            break;
        default:
            break;
    }
    g_Button2State = next2State;
    printf("next2State = %d\n", next2State);
}
void button_gpio_io_init(void)
{
    //初始化GPIO
    IoTGpioInit(HI_GPIO_IDX_5);
    //GPIO-5按键控制加热丝
    hi_io_set_func(HI_IO_NAME_GPIO_5, HI_IO_FUNC_GPIO_5_GPIO);    
    //将GPIO5设置为输入
    IoTGpioSetDir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
    //GPIO5设置上拉状态，无信号输入时为高电平
    hi_io_set_pull(HI_GPIO_IDX_5, 1);
    //按键执行函数
    IoTGpioRegisterIsrFunc(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE,
                           HI_GPIO_EDGE_FALL_LEVEL_LOW,
                           OnButtonPressed, //中断回调函数
                           NULL);
    //GPIO-7按键选择模式
    hi_io_set_func(HI_IO_NAME_GPIO_7, HI_IO_FUNC_GPIO_7_GPIO);    
    hi_gpio_set_dir(HI_GPIO_IDX_7, HI_GPIO_DIR_IN);
    hi_io_set_pull(HI_GPIO_IDX_7, 1);
    IoTGpioRegisterIsrFunc(HI_GPIO_IDX_7, HI_INT_TYPE_EDGE,
                           HI_GPIO_EDGE_FALL_LEVEL_LOW,
                           OnButton2Pressed,
                           NULL);
}

static unsigned int g_MonitorTask_w;
const hi_task_attr MonitorTaskAttr_w = {
    .task_prio = 20,
    .stack_size = 4096,
    .task_name = "BuggyNetworkMonitorTask",
};
void *MonitorButtonTask_t(void * para)  //按键控制加热丝及菜单显示
{
    while(1)
    {
        switch(g_ButtonState)
        {
            case HEATER_ON:
                if(g_Button2State == HEATING_MODE)
                    heater_ON();
                else if(g_Button2State == SETTING)
                    setting_ON();
                else if(g_Button2State == TEMP_UP)
                    setting_tempUP();
                else if(g_Button2State == TEMP_DOWN)
                    setting_temp_DOWN();
                else if(g_Button2State == SAVE)
                    setting_SAVE();
                break;
            case HEATER_OFF:
                if(g_Button2State == HEATING_MODE)
                    heater_OFF();
                else if(g_Button2State == SETTING)
                    setting_ON();
                else if(g_Button2State == TEMP_UP)
                    setting_tempUP();
                else if(g_Button2State == TEMP_DOWN)
                    setting_temp_DOWN();
                else if(g_Button2State == SAVE)
                    setting_SAVE();
                break; 
        }
    }
    return NULL;
}
void *MonitorLedTask_t(void * para) //LED根据温度变化以及报警电路报警
{
    while(1){
        extern float Temperature;
        if(Temperature > TEMP_MAX){
            IoTGpioSetOutputVal(HI_GPIO_IDX_6,1);       //报警持续3秒
            sleep(3);
            IoTGpioSetOutputVal(HI_GPIO_IDX_6,0);       //关闭报警

            sleep(3);
            heater_OFF();   //加热丝关闭
            g_ButtonState = HEATER_OFF;
            printf("\r\n----warning! The heater turn OFF!\r\n\r\n");
            sleep(TEMP_WAITTIME);  //等待温度降下
        }
        else{
            IoTGpioSetOutputVal(HI_GPIO_IDX_6,0);       //亮绿灯
        }
        sleep(2);
    }
    return NULL;
}

hi_void button_led_demo(hi_void)
{    
    button_gpio_io_init();
    SHT3X_init();
    warning_gpio_io_init();
    heater_gpio_io_init();
    oled_gpio_io_init();

    hi_task_create(&g_MonitorTask_w, // task标识 //
        &MonitorTaskAttr_w,
        MonitorButtonTask_t, // task处理函数 //
        NULL); // task处理函数参数 //
    
    hi_task_create(&g_MonitorTask_w,
        &MonitorTaskAttr_w,
        MonitorLedTask_t,
        NULL);

    return;
}

//华为云控制用
void heater_ctrl_ON(void)
{
    heater_ON();
    g_ButtonState = HEATER_ON;
}
void heater_ctrl_OFF(void)
{
    heater_OFF();
    g_ButtonState = HEATER_OFF;
}
/*//设置部分存在bug,不能配合界面显示
void setting_ctrl_UP(void)
{
    TEMP_MAX++;
    g_Button2State == TEMP_UP;
    sleep(3);
    g_Button2State = SETTING;
}
void setting_ctrl_DOWN(void)
{
    TEMP_MAX--;
    g_Button2State == TEMP_DOWN;
    sleep(3);
    g_Button2State = SETTING;
}
*/